//
//  FSSpecialTopicNoticeViewController.m
//  fanshu
//
//  Created by 许海峰 on 16/4/9.
//  Copyright © 2016年 FS. All rights reserved.
//

#import "FSSpecialTopicNoticeViewController.h"
#import "FSNoticeSettingItem.h"
#import "FSNoticeSettingSwitchCell.h"


@interface FSSpecialTopicNoticeViewController ()
@property (nonatomic,strong) NSMutableArray *noticeSettingItemArray;

@end
static NSString * const IDSwitch = @"switchCell";
@implementation FSSpecialTopicNoticeViewController
- (NSMutableArray *)noticeSettingItemArray
{
    if (_noticeSettingItemArray == nil) {
        _noticeSettingItemArray = [NSMutableArray array];
    }
    return _noticeSettingItemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"关注的话题";
    self.navigationItem.rightBarButtonItem = nil;
    
    //调整(iOS7以上)表格分隔线
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        self.tableView.separatorInset = UIEdgeInsetsZero;
    }
//    //调整(iOS8以上)表格分隔线
//    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
//        self.tableView.layoutMargins = UIEdgeInsetsZero;
//    }
    //联网加载数据
    [self loadWebData];
    [self.tableView registerClass:[FSNoticeSettingSwitchCell class] forCellReuseIdentifier:IDSwitch];
    
    [self setupTableViewHeader];
    
}
-(void)setupTableViewHeader
{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 45)];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, ScreenWidth-20, 10)];
    [header addSubview:titleLabel];
    titleLabel.text = @"管理更新推送专题";
    titleLabel.font = [UIFont systemFontOfSize:12];
    titleLabel.textColor = [UIColor orangeColor];
    
    UILabel *detialLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(titleLabel.frame), ScreenWidth-20, 25)];
    
    detialLabel.text = @"您可以在此管理需要推送更新的专题";
    detialLabel.numberOfLines = 0;
    detialLabel.font = [UIFont systemFontOfSize:12];
    detialLabel.textColor = [UIColor darkGrayColor];
    [header addSubview:detialLabel];
    
    UIView *lineH = [[UIView alloc] initWithFrame:CGRectMake(0, 44.5, ScreenWidth, 0.5)];
    lineH.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.7];
    [header addSubview:lineH];
    self.tableView.tableHeaderView = header;
    
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
    UIView *lineF = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.5)];
    lineF.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.7];
    [footer addSubview:lineF];
    self.tableView.tableFooterView = footer;
}
-(void)loadWebData
{
    //第一组
    FSNoticeSettingItem *item0 = [[FSNoticeSettingItem alloc] init];
    item0.title = @"话题1";
    item0.indicatorRight = NO;
    item0.switchOn = YES;
    item0.icon = @"icon_comment_emoji";
    [self.noticeSettingItemArray addObject:item0];
    
    
    //第二组
    FSNoticeSettingItem *item1 = [[FSNoticeSettingItem alloc] init];
    item1.title = @"话题2";
    item1.indicatorRight = NO;
    item1.switchOn = YES;
    item1.icon = @"icon_comment_emoji";
    [self.noticeSettingItemArray addObject:item1];
}
//监听cell里面开关状态
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"noticeSettingItemSwitchChange" object:nil];
}
-(void)switchChange:(NSNotification *)note
{
    //发出通知的item
    FSNoticeSettingItem *cellItem = note.object;
    //如果开关被打开
    if (cellItem.switchOn == YES) {
        NSLog(@"%@-开关打开",cellItem.title);
    } else {
        NSLog(@"%@-开关关闭",cellItem.title);
    }
    //延迟刷新,否则会造成UISwitch动画被打断
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.tableView reloadData];
    });
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.noticeSettingItemArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FSNoticeSettingSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:IDSwitch forIndexPath:indexPath];
    FSNoticeSettingItem *item = self.noticeSettingItemArray[indexPath.row];
    cell.item = item;
    //移除,重新监听
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"noticeSettingItemSwitchChange" object:item];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchChange:) name:@"noticeSettingItemSwitchChange" object:item];
    

    return cell;
}

#pragma mark - Table view delegate
-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
